from rest_framework import serializers
from .models import HrChatMessage, HrChatSession


class HrChatSerializer(serializers.Serializer):
    """企业人事助手聊天请求序列化器"""
    prompt = serializers.CharField(
        required=True,
        min_length=1,
        error_messages={"required": "请输入人事咨询", "min_length": "人事咨询不能为空"}
    )
    user_id = serializers.CharField(required=False, default="123456789")  # 可选，默认用户ID


class HrImageUploadSerializer(serializers.Serializer):
    """企业人事助手图片上传序列化器"""
    image = serializers.ImageField(required=True, error_messages={"required": "请上传图片"})
    user_id = serializers.CharField(required=False, default="123456789")  # 可选，默认用户ID


# 消息序列化器（嵌套在会话中）
class HrChatMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = HrChatMessage
        fields = ['id', 'sender', 'content', 'image', 'created_at', 'tokens_used']
        read_only_fields = fields  # 所有字段都是只读的


# 会话序列化器（包含消息列表）
class HrChatSessionSerializer(serializers.ModelSerializer):
    messages = HrChatMessageSerializer(many=True, read_only=True)  # 嵌套消息列表

    class Meta:
        model = HrChatSession
        fields = ['id', 'title', 'created_at', 'updated_at', 'is_archived', 'messages']
        read_only_fields = fields